import { ReactNode, useState } from 'react'
import { Button, Space, message } from 'antd';
import { VideoCourtSchema, VideoDownloadSchema } from '@src/components/schema/videoCourt'
import { awaitWrap, confirmAction } from '@src/assets/js/tool'
import { updateRequest, getDocRequest, getVideoRequest, VideoSchema } from './ajax'
import moment from 'moment'
import { download } from '@src/assets/js/downloadRename';
import InfoModal from '../infoModal'
import DownloadModal from '../downloadModal'

function startMeeting (d: { [k: string]: any }, refreshFn: () => void) {
  updateMeetingHandle({
    id: d.id,
    status: '会议开始',
      meeting_begin_datetime: Math.floor(moment().valueOf() / 1000)
  }, refreshFn)
}

function endMeeting (d: { [k: string]: any }, refreshFn: () => void) {
  updateMeetingHandle({
    id: d.id,
    status: '会议结束',
    meeting_end_datetime: Math.floor(moment().valueOf() / 1000)
  }, refreshFn)
}

async function getTemplate (id: string) {
  const hide = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getDocRequest(id))
  hide()
  if (e === null && d !== null) {
    download(d.docx_path, '笔录模板.docx')
  }
}

function toVideo (d: VideoCourtSchema | { [k: string]: any }) {
  if (!d.chair_joinuri) {
    return message.warning('视频庭审地址不存在')
  }
  window.open(d.chair_joinuri, '_blank')
}

function completeMeeting (d: { [k: string]: any }, refreshFn: () => void) {
  updateMeetingHandle({
    id: d.id,
    status: '完成'
  }, refreshFn)
}

async function updateMeetingHandle (d: { [k: string]: any }, refreshFn: () => void) {
  const hide = message.loading('提交中', 120)
  const [e] = await awaitWrap(updateRequest(d))
  hide()
  if (e === null) {
    refreshFn()
  }
}

async function downloadVideo (id: string, setList: (v: VideoSchema[]) => void, setVisible: (v: boolean) => void) {
  const hide = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getVideoRequest(id))
  hide()
  if (e === null && d !== null) {
    if (d.list.length === 0) {
      return message.warning('暂无视频文件')
    }
    setList(d.list)
    setVisible(true)
  }
}

interface Props {
  meetingData: VideoCourtSchema | { [k: string]: any }
  addHandle: () => void
  refreshFn: () => void
  showUpload: () => void
}

function Main ({ meetingData: d, addHandle, refreshFn, showUpload }: Props) {
  const [infoVisible, setInfoVisible] = useState(false)
  const [downloadVisible, setDownloadVisible] = useState(false)
  const [downloadList, setDownloadList] = useState<VideoSchema[]>([])
  const files = d.meeting_file || []
  const hasFile = files.length > 0
  const s = d.status
  const btns: ReactNode[] = []
  if (['预约', '变更'].indexOf(s) !== -1) {
    const m = '您确定要开始信息后，系统会初始化会议账号，并通知相关参会人员。（账号为DL+手机号，密码为DL+手机号后6位）是否确认会议信息？'
    btns.push(<Button key={0} type='primary' ghost onClick={() => addHandle()}>新增参会人员</Button>)
    btns.push(<Button key={1} type='primary' onClick={() => confirmAction(m, () => startMeeting(d, refreshFn))}>确认会议人员信息</Button>)
  }
  if (['会议开始'].indexOf(s) !== -1) {
    btns.push(<Button key={0} type='primary' onClick={() => confirmAction('您确定要结束会议吗？', () => endMeeting(d, refreshFn))}>结束会议</Button>)
    btns.push(<Button key={1} type='primary' ghost onClick={() => setInfoVisible(true)}>会议信息</Button>)
    btns.push(<Button key={2} onClick={() => getTemplate(d.id)}>下载笔录模板</Button>)
    btns.push(<Button key={3} onClick={() => toVideo(d)}>进入视频庭审</Button>)
  }
  if (['会议结束'].indexOf(s) !== -1) {
    btns.push(<Button key={0} type='primary' disabled={!hasFile} onClick={() => confirmAction('会议完成后将无法再签署笔录，您是否确定要完成会议', () => completeMeeting(d, refreshFn))}>完成会议</Button>)
    btns.push(<Button key={1} type='primary' ghost onClick={() => showUpload()}>上传笔录</Button>)
    btns.push(<Button key={2} onClick={() => getTemplate(d.id)}>下载笔录模板</Button>)
    btns.push(<Button key={3} onClick={() => downloadVideo(d.id, setDownloadList, setDownloadVisible)}>查看录像</Button>)
  }
  if (['完成'].indexOf(s) !== -1) {
    btns.push(<Button key={2} onClick={() => downloadVideo(d.id, setDownloadList, setDownloadVisible)}>查看录像</Button>)
  }
  return (
    <span>
      <DownloadModal visible={downloadVisible} hide={() => setDownloadVisible(false)} data={downloadList}  />
      <InfoModal data={d} visible={infoVisible} hide={() => setInfoVisible(false)} />
      <Space>
        {btns}
      </Space>
    </span>
  )
}

export default Main
